package com.duoec.magichour.controller.api;

import com.duoec.magichour.core.annotation.Access;
import com.duoec.magichour.core.web.BaseApiController;
import com.duoec.magichour.dto.BaseResponse;
import com.duoec.magichour.dto.UserInfo;
import com.duoec.magichour.dto.request.ArticleCommentSave;
import com.duoec.magichour.dto.response.ArticleCommentDto;
import com.duoec.magichour.enums.RoleEnum;
import com.duoec.magichour.pojo.ArticleComment;
import com.duoec.magichour.service.ArticleCommentService;
import com.duoec.magichour.service.ArticleService;
import com.duoec.magichour.service.CaptchaService;
import com.duoec.magichour.service.SensitiveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章评论接口
 */
@RestController
@RequestMapping("/api/article-comment/{articleId:\\d+}")
public class ArticleCommentApiController extends BaseApiController {
    @Autowired
    private ArticleCommentService articleCommentService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private SensitiveWordService sensitiveWordService;

    @Autowired
    private CaptchaService captchaService;

    /**
     * 添加评论
     *
     * @param articleId 文章ID
     * @param request   添加评论的内容
     * @return
     */
    @Access
    @RequestMapping(method = RequestMethod.POST, value = "/comment")
    public BaseResponse<ArticleCommentDto> comment(
            @PathVariable long articleId,
            @RequestBody ArticleCommentSave request
    ) {
        //参数较验
        if (articleId <= 0) {
            return BaseResponse.error(BaseResponse.CODE_PARAM_INVALID, "文章ID不能为空！");
        }

        //验证码
        if(!captchaService.validate(request.getCaptcha())) {
            return BaseResponse.error(BaseResponse.CODE_PARAM_INVALID, "验证码错误！");
        }

        if (!articleService.exists(articleId)) {
            return BaseResponse.error(BaseResponse.CODE_NOT_FIND, "文章不存在或已被删除！");
        }

        if(!sensitiveWordService.checkPass(request.getComment())) {
            return BaseResponse.error(BaseResponse.CODE_PARAM_INVALID, "评论内容包含敏感信息！");
        }

        UserInfo userInfo = getUserInfo();
        ArticleCommentDto articleComment = articleCommentService.save(userInfo.getId(), articleId, request);
        return BaseResponse.success(articleComment);
    }

    /**
     * 获取某篇文章的评论
     *
     * @param articleId 文章ID
     * @param pageNo    当前分页数，1为第一页
     * @param pageSize  每页最大记录数
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/comments")
    public BaseResponse<List<ArticleCommentDto>> getComments(
            @PathVariable long articleId,
            @RequestParam int pageNo,
            @RequestParam int pageSize
    ) {
        List<ArticleCommentDto> comments = articleCommentService.list(articleId, pageNo, pageSize);
        return BaseResponse.success(comments);
    }

    /**
     * 删除某篇文章的某个评论
     *
     * @param articleId 文章ID
     * @param commentId 评论ID
     * @return 返回删除成功的评论ID
     */
    @Access(RoleEnum.ADMIN)
    @RequestMapping(method = RequestMethod.DELETE, value = "/comment/{commentId:\\d+}")
    public BaseResponse<Long> delete(
            @PathVariable long articleId,
            @PathVariable long commentId
    ) {
        UserInfo userInfo = getUserInfo();
        ArticleComment comment = articleCommentService.get(commentId);
        if(comment.getUserId().equals(userInfo.getId())) {
            //不是自己
        }
        if(!comment.getArticleId().equals(articleId) || !comment.getCommentStatus().equals(0)) {
            return BaseResponse.notFindError("评论不存在或已被删除！");
        }

        articleCommentService.delete(articleId, commentId);
        return BaseResponse.success(commentId);
    }
}
